# OpenSUSE Leap 42.1 YafaRay Blender Add-On building procedure (only valid for YafaRay v3.1.2 or higher)
# NOTE: This procedure generates a "yafaray_v3" folder that can be installed in the Blender Add-Ons folder
#       However, this only works for the Blender included in the distro (currently 2.75 in OpenSUSE Leap 42.1). 
#       When using this yafaray_v3 folder with the Blender downloaded from www.blender.org there might be
#       conflicts with the Blender EXR, PNG and JPEG libraries.

# INSTALLING DEVEL TOOLS
sudo zypper install -y nano git cmake autoconf gcc gcc-c++ cpp swig

# INSTALLING LIBRARY DEPENDENCIES
sudo zypper install -y python3-devel zlib-devel libpng12-devel freetype2-devel libxml2-devel libtiff-devel libjpeg62-devel opencv-devel openexr-devel

# INSTALLING BLENDER
sudo zypper install -y blender 

# DOWNLOADING YAFARAY AND SETTING UP FOLDERS
mkdir -p $HOME/yafa-dev/src $HOME/yafa-dev/make $HOME/yafa-dev/build
cd $HOME/yafa-dev/src
git clone https://github.com/YafaRay/Blender-Exporter
git clone https://github.com/YafaRay/Core

# CREATE UserConfig.txt FILE WITH YAFARAY BUILDING OPTIONS. Execute the entire block below until (including) EOF

cat <<EOF >$HOME/yafa-dev/make/UserConfig.txt
####################################################################
# UserConfig File to build for OpenSuse Leap 42.1 Blender Add-On
####################################################################
# WARNING: DON'T TOUCH THE NEXT LINE                                           
set(YAF_USER_CONFIG ON)
####################################################################

set(DEBUG_BUILD OFF)
set(USER_INSTALL_PREFIX "$HOME/yafa-dev/build/yafaray_v3/")
set(BLENDER_ADDON ON)
set(YAF_PY_VERSION 3.4)
set(BLENDER_EXPORTER_PATH "$HOME/yafa-dev/src/Blender-Exporter")
####################################################################
EOF


# BUILDING YAFARAY
cd $HOME/yafa-dev/make
cmake $HOME/yafa-dev/src/Core
# Note: "-j9" uses 9 threads in this example for speeding up the building process. It can be changed to "-j<number of threads>" depending on the number of cores of the processor used for building YafaRay.
make -j9
make install -j9

# TEST IN BLENDER
mkdir -p $HOME/.config/blender/2.75/scripts/addons
cp -R $HOME/yafa-dev/build/yafaray_v3 $HOME/.config/blender/2.75/scripts/addons/yafaray_v3
blender $HOME/yafa-dev/build/yafaray_v3/tests/test01/test01.blend

# In Blender, go to File->User Preferences->AddOns-->YafaRay v3 and enable it. Then, click save settings and close the User Preferences window.

# Now you should be able to render the test01.blend file.
